package com.junsi.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

public class InteractiveUtil {
	
	/**
	 * 把List集合转为JSON并写入到前端
	 * @param list
	 * @param response	
	 * @throws IOException
	 */
	public static void WriteJsonForHtml(List<?> list,HttpServletResponse response) {
		JSONArray jsonArray = JSONArray.fromObject(list);
		PrintWriter writer;
		try {
			writer = response.getWriter();
			writer.write(jsonArray.toString());
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
	public static void WriteBeanForHtml(Object object,HttpServletResponse response ){
		JSONArray jsonArray = JSONArray.fromObject(object);
		PrintWriter writer;
		try {
			writer = response.getWriter();
			writer.write(jsonArray.toString());
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void WriteJsonForPage(List<?> list,PageUtil page,HttpServletResponse response) {
		PageJSONBean pageJSONBean = new PageJSONBean(list, page.getPageCount(), page.getPageNum());
		JSONArray jsonArray = JSONArray.fromObject(pageJSONBean);
		PrintWriter writer;
		try {
			writer = response.getWriter();
			writer.write(jsonArray.toString());
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void WriteStringForHtml(String string,HttpServletResponse response ){
		PrintWriter writer;
		try {
			writer = response.getWriter();
			writer.write(string);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}
